﻿using STHPMS1.Common.C_Entity;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Core;
using STHPMS1.ICore.I_DAL;
using STHPMS1.ICore.I_Entity.Stmpms1;
using STHPMS1.ICore.I_Serv;
using STHPMS1.ICore.I_UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace STHPMS1.Web.W_Controller.Decision.Fondos
{
    [RoutePrefix("api/dfseclassproj")]
    public class DF_SeclassprojController : STHPMS1.Web.W_Core.MyController
    {
        [Route("")]
        [Route("page/{page?}", Name = "GetdfseclassprojByPage")]
        [Route("rows/{rows?}")]
        [Route("page/{page?}/rows/{rows?}")]
        public IHttpActionResult Get(string road = null, int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;

            Expression<Func<Df_Seclass_Proj, bool>> exp = null;
            if (road != null && road.ToString().Length > 0)
                exp = pre => pre.Anni_Name == road.ToString();

            var data = this.Scene.Bll.Df_Seclass_Proj.Get<Df_Seclass_Proj>(exp, info).ToArray();
            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }

        /// <summary>
        /// 同步数据 养护方案统计
        /// </summary>
        /// <param name="anniname"></param>
        [Route("SetInsert")]
        [HttpGet]
        public void SetInsert(string anniname)
        {
            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;

            dao.Delete<Df_Seclass_Proj>(p => p.Anni_Name == anniname);

            Expression<Func<Df_Costmod_Para, bool>> exp = null;
            if (anniname != null && anniname.ToString().Length > 0)
                exp = pre => pre.Anni_Name == anniname.ToString();

            List<Df_Costmod_Para> data = this.Scene.Bll.Df_Costmod_Para.Get<Df_Costmod_Para>(exp, null).ToList();  //资金模型参数

            foreach (Df_Costmod_Para info in data)
            {
                
                //List<int> list = this.Scene.Bll.Df_Seclass_Proj.GetRD_ClassList(anniname);
                List<int> list = dao.Df_Seclass_Proj.GetRD_ClassList(anniname);
                foreach(int rdclass in list)
                //for (int rdclass = 160; rdclass <= 164; rdclass++)  //公路等级
                {
                    Df_Seclass_Proj seclassinfo = new Df_Seclass_Proj();
                    seclassinfo.Id = serv.NewGuid();
                    seclassinfo.Anni_Name = info.Anni_Name;
                    seclassinfo.Roadnet_Name = info.Roadnet_Name;
                    seclassinfo.Analy_Year = info.Analy_Year;
                    seclassinfo.Rd_Class = rdclass;
                    seclassinfo.Dailymain_Cost = dao.Df_Seclass_Proj.GetSegid_MainCOST(info.Anni_Name, info.Analy_Year, rdclass, 12000);
                    seclassinfo.Dailymain_Leg = dao.Df_Seclass_Proj.GetRoadLenght(info.Anni_Name, info.Analy_Year, rdclass, 12000) / 1000;

                    seclassinfo.Premain_Cost = dao.Df_Seclass_Proj.GetSegid_MainCOST(info.Anni_Name, info.Analy_Year, rdclass, 12001);
                    seclassinfo.Premain_Leg = dao.Df_Seclass_Proj.GetRoadLenght(info.Anni_Name, info.Analy_Year, rdclass, 12001) / 1000;

                    seclassinfo.Medmian_Cost = dao.Df_Seclass_Proj.GetSegid_MainCOST(info.Anni_Name, info.Analy_Year, rdclass, 12002);
                    seclassinfo.Medmian_Leg = dao.Df_Seclass_Proj.GetRoadLenght(info.Anni_Name, info.Analy_Year, rdclass, 12002) / 1000;

                    seclassinfo.Majmain_Cost = dao.Df_Seclass_Proj.GetSegid_MainCOST(info.Anni_Name, info.Analy_Year, rdclass, 12003);
                    seclassinfo.Majmain_Leg = dao.Df_Seclass_Proj.GetRoadLenght(info.Anni_Name, info.Analy_Year, rdclass, 12003) / 1000;

                    //this.Scene.Bll.Df_Seclass_Proj.Save<Df_Seclass_Proj>(seclassinfo);
                    dao.Save<Df_Seclass_Proj>(seclassinfo);
                }
            }
        }

    }
}